package com.tsxy.controller.administrator;

import com.tsxy.common.APIResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tsxy.entity.Address;
import com.tsxy.entity.OrderDetails;
import com.tsxy.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.tsxy.service.IOrderService;
import com.tsxy.entity.Order;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 飞
 * @since 2022-03-24
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private IOrderService orderService;
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderDetailsMapper orderDetailsMapper;
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private UsedBookMapper usedBookMapper;

    //新增或者修改
    @PostMapping
    public APIResult save(@RequestBody Order order){
        return APIResult.success(orderService.saveOrUpdate(order));
    }
    //根据id删除
    @DeleteMapping("/{id}")
    public APIResult delete(@PathVariable Integer id) {
        return APIResult.success(orderService.removeById(id));
    }
    //批量删除
    @PostMapping("/del/batch")
        public APIResult deleteBath(@RequestBody List<Integer> ids){
        return APIResult.success(orderService.removeByIds(ids));
    }
    //查询所有
    @GetMapping
    public APIResult findAll() {
        return APIResult.success(orderService.list());
    }
    //根据id查询
    @GetMapping("/{id}")
        public APIResult findOne(@PathVariable Integer id) {
        return APIResult.success(orderService.getById(id));
    }
    //分页查询
    @GetMapping("/page")
    public APIResult findPage(@RequestParam Integer pageNum,
                    @RequestParam Integer pageSize) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("status");
        Page<Order> page = orderService.page(new Page<>(pageNum, pageSize), queryWrapper);
        for (Order record : page.getRecords()) {
            record.setUser(userMapper.selectById(record.getUserId()));
            record.setAddress(addressMapper.selectById(record.getAddressId()));
            record.setOrderDetails(orderDetailsMapper.selectList(new QueryWrapper<OrderDetails>().eq("order_id", record.getId())));
            for (OrderDetails orderDetail : record.getOrderDetails()) {
                if ("0".equals(orderDetail.getBookType())){
                    orderDetail.setBook(bookMapper.selectById(orderDetail.getBookId()));
                }else {
                    orderDetail.setUsedBook(usedBookMapper.selectById(orderDetail.getBookId()));
                }
            }
        }
        return APIResult.success(page);
    }



}

